package com.company.huisuo.repository

import com.company.huisuo.biz.bean.AppGameStartReqVO
import com.company.huisuo.biz.bean.AppGameLotteryReqVO
import com.company.huisuo.biz.bean.AppGameParticipantReqVO
import com.company.huisuo.biz.bean.GameStartResponse
import com.company.huisuo.biz.bean.GameLotteryResponse
import com.company.huisuo.biz.bean.GamePlayRecordResponse
import com.company.huisuo.biz.bean.GameParticipantListResponse
import com.company.huisuo.biz.bean.DeleteGameParticipantResponse
import com.company.huisuo.biz.bean.NotParticipantListResponse
import com.company.huisuo.network.RetrofitClient
import com.company.huisuo.network.api.GameApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * 游戏数据仓库
 */
class GameRepository {
    
    private val gameApi: GameApi by lazy {
        RetrofitClient.createService()
    }
    
    /**
     * 游戏开始
     * @param sessionId 场次id
     * @param gameId 游戏id
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun gameStart(
        sessionId: Long,
        gameId: Long,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<Long> = withContext(Dispatchers.IO) {
        try {
            val request = AppGameStartReqVO(sessionId, gameId)
            val response = gameApi.gameStart(request, tenantId, authorization)
            if (response.code == 0) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "游戏开始失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 获取游戏抽奖结果
     * @param gamePlayRecordId 游戏游玩记录id 开始游戏返回的id
     * @param participantId 参与者id 亲昵空间必传
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun gameLottery(
        gamePlayRecordId: Long,
        participantId: Long? = null,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<com.company.huisuo.biz.bean.AppGameAwardRespVO> = withContext(Dispatchers.IO) {
        try {
            val request = AppGameLotteryReqVO(gamePlayRecordId, participantId)
            val response = gameApi.gameLottery(request, tenantId, authorization)
            if (response.code == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "游戏抽奖失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 添加游戏参与人
     * @param gamePlayRecordId 游戏游玩记录id 开始游戏返回的id
     * @param customerId 客户id
     * @param customerName 客户名称
     * @param artistId 艺人id
     * @param artistName 艺人名称
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun addGameParticipant(
        gamePlayRecordId: Long,
        customerId: Long,
        customerName: String,
        artistId: Long,
        artistName: String,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<Long> = withContext(Dispatchers.IO) {
        try {
            val request = AppGameParticipantReqVO(gamePlayRecordId, customerId, customerName, artistId, artistName)
            val response = gameApi.addGameParticipant(request, tenantId, authorization)
            if (response.code == 0) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "添加游戏参与人失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 获取游戏游玩记录
     * @param gameId 游戏ID
     * @param sessionId 会话ID
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun getPlayRecord(
        gameId: Long,
        sessionId: Long,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<com.company.huisuo.biz.bean.AppGamePlayRecordRespVO> = withContext(Dispatchers.IO) {
        try {
            val response = gameApi.getPlayRecord(gameId, sessionId, tenantId, authorization)
            if (response.code == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "获取游戏游玩记录失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 获取游戏参与人列表
     * @param gamePlayRecordId 游戏游玩记录ID
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun getGameParticipantList(
        gamePlayRecordId: Long,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<List<com.company.huisuo.biz.bean.AppGameParticipantRespVO>> = withContext(Dispatchers.IO) {
        try {
            val response = gameApi.getGameParticipantList(gamePlayRecordId, tenantId, authorization)
            if (response.code == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "获取游戏参与人列表失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 删除游戏参与人
     * @param participantId 参与人ID
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun deleteGameParticipant(
        participantId: Long,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<Boolean> = withContext(Dispatchers.IO) {
        try {
            val response = gameApi.deleteGameParticipant(participantId, tenantId, authorization)
            if (response.code == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "删除游戏参与人失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 获取未参与游戏艺人列表
     * @param gamePlayRecordId 游戏游玩记录ID
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun getNotParticipantList(
        gamePlayRecordId: Long,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<List<com.company.huisuo.biz.bean.AppGameNotParticipantRespVO>> = withContext(Dispatchers.IO) {
        try {
            val response = gameApi.getNotParticipantList(gamePlayRecordId, tenantId, authorization)
            if (response.code == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "获取未参与游戏艺人列表失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
}